package com.example.hello;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpRequest {

    static String website = "http://localhost:8080/";

    public static String GET(String collection) {
        return executeGet(website, collection);
    }

    public static String executeGet(String targetURL, String collection) {
        String get = "";
        try {
            URL url = new URL(targetURL + collection);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/json");

            connection.connect();

            InputStream is;
            if (connection.getResponseCode() >= 400) {
                is = connection.getErrorStream();
            } else {
                is = connection.getInputStream();
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                get += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return get;
    }

    public static boolean GET(String collection, String json) {
        return executeGet(website, collection, json);
    }

    public static boolean executeGet(String targetURL, String collection, String json) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = new HashMap<>();
        try {
            map = mapper.readValue(json, new TypeReference<Map<String, String>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        String get = executeGet(targetURL, collection);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (!get.contains(entry.getKey() + "\":\"" + entry.getValue()))
                return false;
        }
        return true;
    }

    public static boolean executePost(String collection, String json) {
        return executePost(website, collection, json);
    }

    public static boolean executePost(String targetURL, String collection, String json) {
        boolean success = false;
        try {
            URL url = new URL(targetURL + collection);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestMethod("POST");

            connection.connect();

            OutputStream os = connection.getOutputStream();
            os.write(json.getBytes("UTF-8"));
            os.close();

            InputStream is;
            if (connection.getResponseCode() >= 400) {
                is = connection.getErrorStream();
            } else {
                is = connection.getInputStream();
                success = true;
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

    public static boolean executePut(String collection, String json) {
        return executePut(website, collection, json);
    }

    public static boolean executePut(String targetURL, String collection, String json) {
        boolean success = false;
        try {
            URL url = new URL(targetURL + collection);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestMethod("PUT");

            connection.connect();

            OutputStream os = connection.getOutputStream();
            os.write(json.getBytes("UTF-8"));
            os.close();

            InputStream is;
            if (connection.getResponseCode() >= 400) {
                is = connection.getErrorStream();
            } else {
                is = connection.getInputStream();
                success = true;
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

    public static boolean executeDelete(String collection) {
        return executeDelete(website, collection);
    }

    public static boolean executeDelete(String targetURL, String collection) {
        boolean success = false;
        try {
            URL url = new URL(targetURL + collection);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestMethod("DELETE");

            connection.connect();

            InputStream is;
            if (connection.getResponseCode() >= 400) {
                is = connection.getErrorStream();
            } else {
                is = connection.getInputStream();
                success = true;
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

}